<?php
class Con_file extends Acan{
	public $defaultAct='list';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		$this->afile = Afunc_file();
	}
	function Act_index(){
		Afun_cache_rm('Adev');
		echo 'del Adev';
		include $this->Atpl();
	}
	function Act_list(){
		$ziptype = array('zip','7z','tar');
		$path = realpath(Acan_anmp_path);
		if($_GET['dir']){
			$path = $_GET['dir'];
		}
		$path = str_replace('\\','/',$path);
		if(substr($path,-1,1)!='/'){$path .= "/";}
		if(Afun_get_os()!='linux'){
			if(preg_match('/^([a-zA-Z]:)/i',$path)==1){$path_h = '';}else{$path_h = '/';}
		//	$path = preg_replace('/([a-zA-Z]:)/i','',$path);
		}
		$history = $this->history('dir',$path);
		$data = $this->afile->ascandiro($path,0,2);
		if($this->id=='list_dir'){
			include $this->Atpl('file/'.$this->id);return;
		}
		if($this->id=='list_file'){
			include $this->Atpl('file/'.$this->id);return;
		}
		$prr = explode('/',$path);
		$dirnav = '';
		foreach ($prr as $v) {
			if(empty($v)){continue;}
			if(empty($pplus) && $path_h==''){$path_s = '';}else{$path_s = '/';}
			$pplus .= $path_s.$v;
			$dirnav .= $path_s.'<a href="'.$this->Aactr.'?dir='.$pplus.'">'.$v.'</a>';
		}
		include $this->Atpl();
	}


	function Act_edit(){
		$path = $_GET['file'];
		$charset = $this->id;
		$history = $this->history('file',$path,$charset);
		if(empty($path)){Afun_refresh('请先选择文件','referrer');}
		if($_POST){
			$this->savefile($path,$_POST['note'],$charset);
			Afun_refresh('保存成功','referrer');
		}
		$note = $this->afile->read($path,$charset);
		$note = htmlspecialchars($note);
		$path_bak = Acan_anmp_path.'/acan/temp/bak'.$path;
		if(!file_exists($path_bak)){$path_bak='';}
		include $this->Atpl();
	}
	function Act_save(){
		$path = $_POST['path'];
		$this->savefile($path,$_POST['note'],$_POST['charset']);
		echo '保存成功!';
	}
	//--保存文件
	function savefile($path,$note,$charset){
		$path_bak = Acan_anmp_path.'/acan/temp/bak'.$path;
		$this->afile->mkdir(dirname($path_bak).'/');
		$this->afile->unlink($path_bak);
		$this->afile->copy($path,$path_bak);
		$this->afile->write($path,str_replace("\r\n","\n",$note),$charset);
	}
	//--恢复备份文件
	function Act_resume(){
		$path = $_GET['path'];
		$path_bak = Acan_anmp_path.'/acan/temp/bak'.$path;
		if(!file_exists($path_bak)){
			$msg='备份文件不存在';
		}else{
			unlink($path);
			copy($path_bak,$path);
			$msg='还原成功';
		}
		Afun_refresh($msg,'referrer');
	}
	function Act_add(){
		$type = $_POST['type'];
		$path = $_POST['path'];
		$newname = $_POST['newname'];
		if($type=='dir'){
			$msg = $this->afile->mkdir($path.$newname);
		}elseif($type=='file'){
			$msg = $this->afile->mkfile($path.$newname,$note);
		}
		echo $msg;
	}
	function Act_rename(){
		$type = $_POST['type'];
		$path = $_POST['path'];
		$name = $_POST['name'];
		$newname = $_POST['newname'];
		if(Afun_get_os()!='linux'){$path = Afun_charset($path,'gbk');$name = Afun_charset($name,'gbk');$newname = Afun_charset($newname,'gbk');}
		if(file_exists($path.$newname)){
			$msg = $path.$newname.'已经存在！';
		}else{
			rename($path.$name,$path.$newname);
			$msg = '重名名成功！';
		}
		echo $msg;
	}
	function Act_del(){
		$path = $_GET['path'];
		if(empty($path)){echo '请先选择文件或者文件夹';return;}
		if($this->id=='file'){
			$msg= $this->afile->unlink($path);
		}elseif($this->id=='dir'){
			$msg=$this->afile->rmdir($path);
		}
		echo $msg;
		//Afun_refresh($msg,'referrer');
	}

	function Act_upfile(){
		$path = $_GET['path'];
		if($_POST){
			$path = $_POST['path'];
			$upname = 'file';
			$savepath = Afunc_file()->upfile($upname,$path);
			echo "<script>window.parent.Acan.close('ajax_dev');
				window.parent.Apps.dev.ajax_list('file');
				window.parent.Acan.alert('成功上传至 $savepath',1000);</script>";exit;
		}
		include $this->Atpl();
	}
	//--压缩文件/夹
	function Act_zip(){
	//	7z a -tzip -p111 archive.7z txt.txt
		$data = $_POST;
		$tmp;
		if(is_array($data['zip'])){
			foreach ($data['zip'] as $k => $v) {
				$tmp .= ' '.$data['path'].$v;
			}
			$savename = $data[path].$data[name].".".$data[ziptype];
			$cmd = A_ROOT."../server/tool/7za a -t$data[ziptype] $savename $tmp";
			$cmd = Afun_charset($cmd,'gbk');
			$cmds = Afun_charset(`$cmd`);
			$endtime=gettime();
			$times = round($endtime- Acan_run_startime,6).'秒';
			echo "<p>压缩耗时:$times</p><p>压缩信息:</p><pre>$cmds</pre>";
		}else{
			echo '未选择文件!';
		}
	}
	//--解压文件/夹
	function Act_unzip(){
	//	7z x -tzip -p111 archive.7z
		$data = $_POST;
		$tmp;
		$name = $data['name'];
		$nrr = explode('.',$name);
		$name = $data['path'].$name;
		$type = $nrr[count($nrr)-1];
		if(!empty($name)){
			$cmd = A_ROOT."../server/tool/7za x -y -o$data[unpath] -t$type $name";
			$cmd = Afun_charset($cmd,'gbk');
			$cmds = Afun_charset(`$cmd`);
			$endtime=gettime();
			$times = round($endtime- Acan_run_startime,6).'秒';
			echo "<p>解压耗时:$times</p><p>解压路径:$data[unpath]</p><p>解压信息:</p><pre>$cmds</pre>";
		}else{
			echo '未选择文件!';
		}
	}

	//打开历史
	function history($type,$path,$charset='utf8'){
		$num['file'] = 50;
		$num['dir'] = 50;
		$data = Afun_cache_get('Adev:history');
		$cache = $data->$type;
		$datas = $cache->datas;
		$key = count($datas);
		//--检查是否缓存过
		if(isset($cache->keys[$path])){$key = $cache->keys[$path];}
		$datas[$key]['path']=$path;
		$datas[$key]['time']=time();
		$datas[$key]['times']= $datas[$key]['times']+1;
		$datas[$key]['charset']= 'utf8';
		$datas = Afun_arr_sort($datas,'path','ASC');
		//--删除多余的数组
		if(!empty($datas[$num[$type]])){unset($datas[$num[$type]]);}
		//--重新排列key
		if(!empty($cache->keys)){unset($cache->keys);}
		foreach($datas as $k=>$v){
			$cache->keys[$v['path']] = $k;
		}
		$cache->datas = $datas;
		$data->$type = $cache;
		Afun_cache_set('Adev:history',$data);
		return $data;
	}
	function coding($type=1){
		
	}

}
?>